package pers.yanfan.rocksdb;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.gitee.yanfanvip.ClusterBuild;
import com.gitee.yanfanvip.interfaces.Database;
import com.gitee.yanfanvip.util.IPHelp;
import com.gitee.yanfanvip.util.RandomString;

public class ClusterNode3 {
	
	static Database database;
	
	public static void main(String[] args) throws Exception {
		database = new ClusterBuild("CLUSTER", 13201, IPHelp.getIPPorts(11201, 12201, 13201)).build();
		eventLoop();
	}
	
	static void eventLoop() throws Exception {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        while(true) {
            try {
                System.out.print("> "); System.out.flush();
                String line = in.readLine().toLowerCase();
                if(line.startsWith("quit") || line.startsWith("exit")) { break; }
                String[] command = line.split(" ");
                switch (command[0]) {
					case "get":
						if(command.length < 3){ throw new RuntimeException("bad command"); }
						byte[] bs = database.get(command[1], command[2]);
						if(bs != null){
							System.out.println(new String(bs));
						}else{
							System.out.println("[NULL]");
						}
						break;
					case "put":
						if(command.length < 3){ throw new RuntimeException("bad command"); }
						database.put(command[1], command[2], RandomString.next(50).getBytes());
						break;
					default:
						break;
				}
                
            } catch(Exception e) {
            	e.printStackTrace();
            }
        }
        database.close();
    }

}
